package LeetCode;

import java.util.HashSet;

public class LC_128_LongestConsecutiveSequence {
    public static void main(String[] args) {

    }

    static class Solution {

        public int longestConsecutive(int[] num) {
            if (num == null || num.length == 0) return 0;
            HashSet<Integer> hs = new HashSet<>();
            for (int aNum : num) hs.add(aNum);
            int max = 0;
            for (int aNum : num) {
                if (hs.contains(aNum)) {
                    int count = 1;
                    hs.remove(aNum);
                    int low = aNum - 1;
                    while (hs.contains(low)) {
                        hs.remove(low);
                        low--;
                        count++;
                    }
                    int high = aNum + 1;
                    while (hs.contains(high)) {
                        hs.remove(high);
                        high++;
                        count++;
                    }
                    max = Math.max(max, count);
                }
            }
            return max;
        }

    }
}
